//
//  YMCheckViewController.m
//  YMMedical
//
//  Created by y s on 2023/7/13.
//

#import "YMCheckViewController.h"
#import "YMCheckTopStepView.h"
#import "YMCheckScanViewController.h"
#import "YMSamplingViewController.h"
#import "YMCheckSetOrAddUserInforVC.h"
#import "YMSampleWaitViewController.h"
#import "YMInsertInstrumentVC.h"
#import "YMCheckInformationModel.h"


@interface YMCheckViewController ()

@property (nonatomic, strong) YMBaseViewController *vcCurrent;
@property (nonatomic, strong) YMCheckTopStepView *vStep;

@property (nonatomic, strong) YMCheckScanViewController *vcStep1;
@property (nonatomic, strong) YMCheckSetOrAddUserInforVC *vcStep2;
@property (nonatomic, strong) YMSamplingViewController *vcStep3;
@property (nonatomic, strong) YMSampleWaitViewController *vcStep4;
@property (nonatomic, strong) YMInsertInstrumentVC *vcStep5;

@property (nonatomic, strong) YMCheckInformationModel *checkModel;

@property (nonatomic, assign) BOOL cameraAgain;


@end

@implementation YMCheckViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}

#pragma mark -- UI
- (void)setUI{
    [self.view addSubview:self.vStep];
    WEAK_SELF;
    [SNavUtil leftIcon:self icon:@"tools_nav_left_back" onClick:^{
        if ([weakSelf.vcCurrent isEqual:weakSelf.vcStep1]){
            [SJumpUtil popVC:weakSelf animated:NO];
            [YMBridging goAppTabWithIndex:0];
        }else if ([weakSelf.vcCurrent isEqual:weakSelf.vcStep2]){
            [weakSelf changeStepIndex:1];
        }else if ([weakSelf.vcCurrent isEqual:weakSelf.vcStep3]){
            [weakSelf changeStepIndex:2];
        }else if ([weakSelf.vcCurrent isEqual:weakSelf.vcStep4]){
            [weakSelf changeStepIndex:3];
        }else if ([weakSelf.vcCurrent isEqual:weakSelf.vcStep5]){
            [weakSelf changeStepIndex:4];
        }
    }];
    [self.vStep mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo([SFitUtil fit:64]);
    }];
    
    [self changeStepIndex:1];
}

- (void)changeStepIndex:(NSInteger)index{
    self.vStep.step = index;
    if (_vcCurrent){
        if ([_vcCurrent isEqual:_vcStep3]){
            _vcStep4 = nil;
        }
        if ([_vcCurrent isEqual:_vcStep1]){
            _vcStep2 = nil;
        }
        [_vcCurrent.view removeFromSuperview];
        [_vcCurrent removeFromParentViewController];
        _vcCurrent = nil;
        _vcStep1 = nil;
        _vcStep3 = nil;
        _vcStep5 = nil;
    }
    if (index == 1)
    {
        [self showNavTitle:KLanguage(@"scan_code_txt")];
        self.vcStep1.checkModel = self.checkModel;
        [SNavUtil rightIcon:self icon:@"" onClick:^{
            
        }];
        _vcCurrent = self.vcStep1;
    }else if (index == 2)
    {
        [self showNavTitle:KLanguage(@"tester_and_info_txt")];
        [SNavUtil rightIcon:self icon:@"tools_nav_home_icon" onClick:^{
            [YMBridging goHomeTabWithAlert];
        }];
        self.vcStep2.checkModel = self.checkModel;
        _vcCurrent = self.vcStep2;
    }else if (index == 3)
    {
        [self showNavTitle:KLanguage(@"sampling_2_txt")];
        self.vcStep3.idStr = self.checkModel.minorTermId;
        _vcCurrent = self.vcStep3;
    }else if (index == 4)
    {
        [self showNavTitle:KLanguage(@"wait_txt")];
        self.vcStep4.waitTime = [self.checkModel.waitTime integerValue];
        self.vcStep4.outTime = [self.checkModel.outTime integerValue];
        _vcCurrent = self.vcStep4;
    }else if (index == 5)
    {
        [self showNavTitle:KLanguage(@"insert_device_txt")];
        self.vcStep5.checkModel = self.checkModel;
        _vcCurrent = self.vcStep5;
    }
    
    [self addChildViewController:self.vcCurrent];
    [self.view addSubview:self.vcCurrent.view];
    [self.vcCurrent.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(0);
        make.top.equalTo(self.vStep.mas_bottom).offset([SFitUtil fit:10]);
    }];
}

#pragma mark -- setter and getter
- (YMCheckTopStepView *)vStep{
    if (_vStep == nil){
        _vStep = [[YMCheckTopStepView alloc] init];
        _vStep.step = 1;
    }
    return _vStep;
}
- (YMCheckScanViewController *)vcStep1{
    if (_vcStep1 == nil){
        _vcStep1 = [[YMCheckScanViewController alloc] init];
        WEAK_SELF;
        _vcStep1.finishBlock = ^(YMCheckInformationModel * _Nonnull model) {
            weakSelf.checkModel = model;
            [weakSelf changeStepIndex:2];
        };
    }
    return _vcStep1;
}
- (YMCheckSetOrAddUserInforVC *)vcStep2{
    if (_vcStep2 == nil){
        _vcStep2 = [[YMCheckSetOrAddUserInforVC alloc] init];
        WEAK_SELF;
        _vcStep2.finishBlock = ^(YMCheckInformationModel * _Nonnull model) {
            weakSelf.checkModel = model;
            [weakSelf changeStepIndex:3];
        };
    }
    return _vcStep2;
}

- (YMSamplingViewController *)vcStep3{
    if (_vcStep3 == nil){
        _vcStep3 = [[YMSamplingViewController alloc] init];
        WEAK_SELF;
        _vcStep3.finishBlock = ^(NSString * _Nonnull collectDate) {
            weakSelf.checkModel.collectDate = collectDate;
            [weakSelf changeStepIndex:4];
        };
    }
    return _vcStep3;
}
- (YMSampleWaitViewController *)vcStep4{
    if (_vcStep4 == nil){
        _vcStep4 = [[YMSampleWaitViewController alloc] init];
        WEAK_SELF;
        _vcStep4.finishBlock = ^{
            [weakSelf changeStepIndex:5];
        };
        _vcStep4.timeOutBlock = ^{
            [weakSelf changeStepIndex:3];
        };
    }
    return _vcStep4;
}
- (YMInsertInstrumentVC *)vcStep5{
    if (_vcStep5 == nil){
        _vcStep5 = [[YMInsertInstrumentVC alloc] init];
    }
    return _vcStep5;
}
- (YMCheckInformationModel *)checkModel{
    if (_checkModel == nil){
        _checkModel = [[YMCheckInformationModel alloc] init];
        _checkModel.deviceId = [YMMyModel shared].deviceId;
    }
    return _checkModel;;
}


@end
